package com.cortex.pineapple;


import java.util.ArrayList;
import java.util.List;

import android.app.Application;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.DisplayMetrics;
import android.widget.Toast;

import com.cortex.pineapple.database.DataBase;
import com.cortex.pineapple.database.VisitLink;
import com.cortex.pineapple.fetch.FetchVisitLink;
import com.cortex.pineapple.fetch.FetchVisitLink.OnFetchVisitLinkChangedListener;
import com.cortex.pineapple.fetch.SearchBing;
import com.cortex.pineapple.fetch.SearchBing.OnFetchBingStateChangedListener;
import com.cortex.pineapple.fetch.FetchIcons;
import com.cortex.pineapple.fetch.FetchIcons.OnFetchIconsChangedListener;
import com.cortex.pineapple.fetch.FetchImages;
import com.cortex.pineapple.fetch.FetchImages.OnFetchImageChangedListener;
import com.cortex.pineapple.fetch.RequestData;
import com.cortex.pineapple.fetch.ResponseData;
import com.cortext.pineapple.link.Link;
import com.cortext.pineapple.link.NewsLink;

public class PineAppleApplication extends Application implements
OnFetchBingStateChangedListener, OnFetchIconsChangedListener, OnFetchImageChangedListener,OnFetchVisitLinkChangedListener{

	private static final boolean IS_TESTING_MODE = false;
	
	private SearchBing searchBing;
	private FetchIcons fetchIcons;
	private FetchImages fetchImages;
	private FetchVisitLink fetchWebDescription;
	
	private OnFetchDataChangedListener onFetchDataChangedListener;
	private OnFetchIconsChangedListener onFetchIconsChangedListener;
	private OnFetchImagesChangedListener onFetchImagesChangedListener;
	private OnFetchVisitLinkChangedListener onFetchVisitLinkChangedListener;

	private DataBase dataBase;
	
	public PineAppleApplication(){
		searchBing = new SearchBing();
		searchBing.setOnFetchBingStateChangedListener(this);
		fetchIcons = new FetchIcons();
		fetchIcons.setOnFetchIconsChangedListener(this);
		fetchImages = new FetchImages();
		fetchImages.setOnFetchImageChangedListener(this);
		fetchWebDescription = new FetchVisitLink();
		fetchWebDescription.setOnFetchWebDescriptionChangedListener(this);
	}
	public void setOnStateLinksChanged(OnFetchDataChangedListener onStateSummaryChanged) {
		this.onFetchDataChangedListener = onStateSummaryChanged;
	}
	public void setOnFetchIconsChangedListener(OnFetchIconsChangedListener onFetchIconsChangedListener) {
		this.onFetchIconsChangedListener = onFetchIconsChangedListener;
	}
	public void setOnFetchImagesChangedListener(OnFetchImagesChangedListener onFetchImagesChangedListener) {
		this.onFetchImagesChangedListener = onFetchImagesChangedListener;
	}
	public void setOnFetchVisitLinkChangedListener(OnFetchVisitLinkChangedListener onFetchVisitLinkChangedListener) {
		this.onFetchVisitLinkChangedListener = onFetchVisitLinkChangedListener;
	}
	public void startFetchingData(RequestData requestData){
		searchBing.startFetch(requestData);
	}
	public void startFetchingIcons(List<? extends Link> links){
		fetchIcons.startFetching((List<Link>) links);
	}
	public void startFetchingImages(List<? extends Link> links){
		fetchImages.startFetching((List<Link>) links);
	}
	public void startFetchingVsitiLink(VisitLink visitLink){
		fetchWebDescription.startFetching(visitLink);
	}
	public void cancelFetchingData(){
		searchBing.cancelFetching();
	}
	public void cancelFetchingIcons(){
		fetchIcons.cancelFetching();
	}
	public void cancelFetchingImages(){
		fetchImages.cancelFetching();
	}
	public DataBase getDataBase(){
		if(dataBase == null){
			dataBase = new DataBase(this);
		}
		return dataBase;
	}
	@Override
	public void bingTimeLoadStarted() {
		if(onFetchDataChangedListener != null){
			onFetchDataChangedListener.dataTimeLoadStarted();
		}
	}
	@Override
	public void bingTimeLoadFinished(ResponseData responseData) {
		if(onFetchDataChangedListener != null ){
			onFetchDataChangedListener.dataTimeLoadFinished(responseData);
		}
	}
	@Override
	public void bingTimeLoadCancelled(String message) {
		Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
		if(onFetchDataChangedListener != null){
			onFetchDataChangedListener.dataTimeLoadCancelled(message);
		}
	}
	
	@Override
	public void timeLoadIconsStarted() {
		if(onFetchIconsChangedListener != null){
			onFetchIconsChangedListener.iconsTimeLoadStarted();
		}
	}
	@Override
	public void timeLoadIconsFinished(List<Link> links) {
		if(onFetchIconsChangedListener != null){
			onFetchIconsChangedListener.iconsTimeLoadFinished(links);
		}
	}
	@Override
	public void timeLoadIconsCancelled() {
		if(onFetchIconsChangedListener != null){
			onFetchIconsChangedListener.iconsTimeLoadCancelled();
		}
	}
	
	@Override
	public void timeLoadImageStarted() {
		if(onFetchImagesChangedListener != null){
			onFetchImagesChangedListener.imagesTimeLoadStarted();
		}
	}
	@Override
	public void timeLoadImageFinished(List<Link> links) {
		if(onFetchImagesChangedListener != null){
			onFetchImagesChangedListener.imagesTimeLoadFinished(links);
		}
	}
	@Override
	public void timeLoadImageCancelled() {
		if(onFetchImagesChangedListener != null){
			onFetchImagesChangedListener.imagesTimeLoadCancelled();
		}
	}
	@Override
	public void visitLinkTimeLoadStarted() {
		if(onFetchVisitLinkChangedListener != null){
			onFetchVisitLinkChangedListener.visitLinkTimeLoadStarted();
		}
	}
	@Override
	public void visitLinkTimeLoadFinished(VisitLink link) {
		if(onFetchVisitLinkChangedListener != null){
			onFetchVisitLinkChangedListener.visitLinkTimeLoadFinished(link);
		}
	}
	@Override
	public void visitLinkTimeLoadCancelled() {
		if(onFetchVisitLinkChangedListener != null){
			onFetchVisitLinkChangedListener.visitLinkTimeLoadCancelled();
		}
	}
	public boolean isInternetConnection(){
		ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
		return networkInfo == null?false:networkInfo.isConnected();

	}
	public interface OnFetchDataChangedListener{
		void dataTimeLoadStarted();
		void dataTimeLoadFinished(ResponseData responseData);
		void dataTimeLoadCancelled(String message);
	}
	public interface OnFetchIconsChangedListener{
		void iconsTimeLoadStarted();
		void iconsTimeLoadFinished(List<Link> links);
		void iconsTimeLoadCancelled();
	}
	public interface OnFetchImagesChangedListener{
		void imagesTimeLoadStarted();
		void imagesTimeLoadFinished(List<Link> links);
		void imagesTimeLoadCancelled();
	}
	public interface OnFetchVisitLinkChangedListener{
		void visitLinkTimeLoadStarted();
		void visitLinkTimeLoadFinished(VisitLink visitLink);
		void visitLinkTimeLoadCancelled();
	}
	public static boolean isTestingMode(){
		return IS_TESTING_MODE;
	}

	



}
